const invoke = require("../dist/invoke");
const service = require("../dist/send-msg");
const action_id = "test_action_id";
const session_id = "test_session_id";
const send_msg = service.send_msg(action_id, session_id);
const dbaccCount = service.db_acc_count(action_id, session_id);
const pyService = service.py_service(action_id, session_id);

function atom(condition) {
	const dask = require("dask");
	const cloneDeep = require("lodash-ts/cloneDeep").default;
	const extend = require("lodash-ts/extend").default;
	const mathjs = require("mathjs");
	const uuid = require("uuid");
	const PARAM_TYPE = ["views", "params", "param"];
	const MODEL = ["model", "simpleRef"];
	const DATA_ARRAY = "dataArray";
	const VIEWS = "views", PARAMS = "params", PARAM = "param", TABLENAME = "tablename", VARIABLE = "variable";

	console.debug || (console.debug = console.log);

	function _list_all_dict(dict_a) {
		let dataAarry = "";
		if (typeof dict_a == "object") {
			for (let key in dict_a) {
				const temp_value = dict_a[key];
				if (key == DATA_ARRAY) {
					dataAarry = temp_value;
					return dataAarry;
				} else if (MODEL.indexOf(key) < 0) {
					`do nothing`;
				} else {
					dataAarry = _list_all_dict(temp_value);
					if (dataAarry) {
						return dataAarry;
					}
				}
			}
		}
		return dataAarry;
	}

	function dataInput(condition) {
		return function (param_type, field_name) {
			if (PARAM_TYPE.indexOf(param_type) < 0) {
				console.error(`please check condtion`);
				return;
			};
			if (!condition[param_type]) {
				console.error(`please check condtion`);
				return;
			};
			const params = condition[param_type];
			let res;
			switch (param_type) {
				case VIEWS:
					params.forEach((view, idx) => {
						if (view[field_name]) {
							const dataArray = _list_all_dict(view);
							if (dataArray.length == 0) {
								res = [];
							}
							res = dataArray;
							return true;
						}
					});
					break;
				case PARAMS:
					params.forEach((param, idx) => {
						if (param[field_name]) {
							const res = params[field_name];
							return true;
						}
					});
					break;
				case PARAM:
					if (params[field_name]) {
						res = [];
						let obj = {};
						obj[field_name] = params[field_name];
						res.push(obj);
					} else {
						console.error(`Not exists this param !!!, expect: ${field_name}`);
						return;
					}
					break;
			}
			return res;
		}
	}

	function _trans_type(value, t_type) {
		try {
			if (t_type == "string") {
				value = value + "";
			} else if (t_type == "int") {
				value = parseInt(value);
			} else if (t_type == "float") {
				value = parseFloat(value);
			} else if (t_type == "timetemp") {
				value = parseFloat(value);
			} else if (t_type == "datastring") {
				value = parseFloat(value);
			} else if (t_type == "boolean" && (value == "false" || value == "False")) {
				value = false;
			} else if (t_type == "boolean" && (value == "true" || value == "True")) {
				value = true;
			}
		} catch (e) {
			console.error("输入数据与选择的格式不匹配");
		}
		return value;
	}

	function generateVariable(condition) {
		return function (data, make_type, field_name, var_name, data_type, get_type) {
			let res;
			if (data.length == 0) {
				res = _no_data(make_type, var_name, get_type);
			} else {
				let field_data = data.map((cur) => {
					return cur[field_name];
				});
				if ((get_type == "first") || (get_type == "last")) {
					res = (get_type == "first") ? field_data[0] : field_data[field_data.length - 1];
				}
				if (get_type == "count") {
					res = field_data.length;
				}
				if ((get_type == "max") || (get_type == "min") || (get_type == "avg") || (get_type == "sum") || (get_type == "columns_to_string")) {
					field_data = field_data.map((cur) => {
						const r = parseFloat(cur);
						if (!r) {
							if (r == 0) {
								return r;
							}
						} else {
							return r;
						}
					});
					if (get_type == "columns_to_string") {
						res = field_data.join(",");
					} else {
						field_data = field_data.sort();
						if (get_type == "max") {
							res = field_data[field_data.length - 1];
						} else if (get_type == "min") {
							res = field_data[0];
						} else {
							const len = field_data.length;
							const sum = field_data.reduce((pre, cur) => {
								return pre + cur;
							}, []);
							res = (get_type == "sum") ? sum : (sum / len);
						}
					}
				}
				res = _trans_type(res, data_type);
				if (make_type != VARIABLE) {
					res = {};
					res[var_name] = value;
				};
			};
			return res;
		}
	}

	function _no_data(make_type, var_name, get_type) {
		let res = {};
		if (make_type == VARIABLE) {
			res = (get_type == "count") ? 0 : "feidao_none";
		} else {
			res = (get_type == "count") ? (res[var_name] = 0) : (res[var_name] = null);
		};
	}

	function readFromDatabase(condition) {
		return async function (query_mode, spaceid, systemid, tablename, fields, criteria, page_no, page_size, sort, sql) {
			const query = [{
				"query_mode": query_mode,
				"spaceid": spaceid,
				"systemid": systemid,
				"tablename": tablename,
				"criteria": criteria,
				"fields": fields,
				"page_no": page_no,
				"page_size": page_size,
				"sort": sort,
				"sql": sql
			}];
			const result = await send_msg("dbacc.find.intranet", query);
			if (result.length == 0) {
				console.error(`get none data`);
				return;
			};
			const res = _docode(result, tablename, fields);
			return res;
		}
	}

	function _docode(views, tablename, fields) {
		for (let i = 0; i < views.length; i++) {
			const view = views[i];
			if (view[TABLENAME] == tablename) {
				const data_array = view[DATA_ARRAY];
				console.log("data_array==", data_array);
				return data_array;
			} else {
				return [];
			}
		};
		return [];
	}

	function get_param_value(condition) {
		return function (field_name, field_value) {
			let res = {};
			res[field_name] = field_value;
			return res;
		}
	}

	function merge_param(condition) {
		return function (...params) {
			let res = params.reduce((pre, cur) => {
				return extend(pre, cur);
			}, {});
			return res;
		}
	}

	function link(condition) {
		return function (...res) {
			return res;
		}
	}

	function cutRow(condition) {
		return function (data, objs, row_num) {
			if (data.length == 0) {
				return data;
			}
			let result = [];
			data.forEach((cur) => {
				let flag = true;
				for (let key in objs) {
					flag = (cur[key] == objs[key]) ? true : false;
					if (!flag) {
						return;
					}
				}
				if (flag) {
					result.push(cur);
				}
			});
			if (row_num == "None" || row_num == 0 || row_num == "") {
				return result;
			} else if (row_num - 1 < result.length) {
				return result.slice(0, row_num);
			} else if (row_num - 1 >= result.length) {
				return result;
			}
		}
	}

	function cutColumn(condition) {
		return function (data, fields) {
			if (fields.length == 0) {
				return data;
			} else {
				const keys = Object.keys(data[0]);
				let no_exit_f = fields.filter((cur) => {
					const idx = keys.indexOf(cur);
					return idx == -1;
				});
				if (no_exit_f.length > 0) {
					const n_f = no_exit_f.join(",");
					console.error(`The columns you want to cut is not exist ${n_f} !!!`)
				} else {
					let result = [];
					data.forEach((cur) => {
						let objs = {};
						for (let i = 0, len = fields.length; i < len; i++) {
							const field = fields[i];
							objs[field] = cur[field];
						}
						result.push(objs);
					});
					return result;
				}
			}
		}
	}

	function generateRow(condition) {
		return function (row_no, field_name, field_value, field_type) {
			try {
				row_no = parseInt(row_no);
				let result = [], objs = {};
				field_name.forEach((cur, idx) => {
					objs[cur] = _trans_type(field_value[idx], field_type[idx]);
				});
				for (let i = 0; i < row_no; i++) {
					result.push(objs);
				}
				return result;
			} catch (error) {
				console.error(`check row no`);
			}
		}
	}

	function generateColumn(condition) {
		return function (data, column_name, column_type, column_value) {
			if (data.length == 0) {
				return data;
			}
			const column_value_type = ["系统日期", "随机值", "GUID"];
			const column_type_type = ["timestamp", "string"];
			const column_type_type_2 = ["int", "string", "float"];
			let objs = {};
			for (let i = 0, len = column_name.length; i < len; i++) {
				const name = column_name[i];
				const type = column_type[i];
				const value = column_value[i];
				if (type == "None") {
					objs[name] = "None";
				}
				if (column_value_type.indexOf(value) > -1) {
					if (value == "系统日期") {
						if (column_type_type.indexOf(type) > -1) {
							const date = new Date();
							if (type == "string") {
								const res = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
								objs[name] = res;
							} else {
								objs[name] = date.getTime();
							}
						} else {
							console.error(`数值类型与列值不匹配`);
						}
					} else if (value == "随机值") {
						if (column_type_type_2.indexOf(type) > -1) {
							let res;
							if (type == "int") {
								res = mathjs.floor(mathjs.random(0, 99));
							} else if (type == "float") {
								res = mathjs.random(0, 99);
							} else {
								res = String.fromCharCode(mathjs.floor(mathjs.random(97, 122)));
							}
							objs[name] = res;
						} else {
							console.error(`数值类型与列值不匹配`);
						}
					} else if (value == "GUID") {
						if (type == "string") {
							objs[name] = uuid.v1();
						} else {
							console.error(`数值类型与列值不匹配`);
						}
					}
				} else {
					console.error(`数值类型与列值不匹配`);
				}
			}
			const c_data = cloneDeep(data);
			c_data.forEach((cur) => {
				extend(cur, objs);
			});
			return c_data;
		}
	}

	function addConstant(condition) {
		return function (data, column_name, column_type, column_value) {
			if (data.length == 0) {
				return data;
			}
			let c_data = cloneDeep(data);
			const f = function (type, value) {
				if (type == "null") {
					return "None";
				} else {
					return _trans_type(value, type);
				}
			}
			let objs = {};
			for (let i = 0, len = column_name.length; i < len; i++) {
				const name = column_name[i];
				const type = column_type[i];
				const value = column_value[i];
				const res = f(type, value);
				objs[name] = res;
			}
			c_data.forEach((cur) => {
				extend(cur, objs);
			});
			return c_data;
		}
	}

	function attr_params(condition) {
		return function (...params) {
			return params;
		}
	}

	function params_format(condition) {
		return function (str, ...params) {
			if (!params) {
				return str;
			}
			let c_str = cloneDeep(str);
			params.forEach((param) => {
				c_str = c_str.replace(" {} ", param);
			});
			return c_str;
		}
	}

	function Str_Date_To_DateTime(str_date) {
		const d = new Date();
		return Date.parse(str_date);
	}

	function coding_rule(length, start, step) {

	}

	function addComputeColumn(condition) {
		return function (data, new_col, new_type, formula, formula_params) {
			if (data.length == 0) {
				return data;
			}
			let c_data = cloneDeep(data);
			c_data.forEach((c) => {
				new_col.forEach((_col, idx) => {
					const _type = new_type[idx]
					const _formula = formula[idx];
					const _params = formula_params[idx];
					const formula_params_2 = _params.replace(/DF/g, "C");
					new Function("C", formula_params_2)(c);
					if (["int", "float"].indexOf(_type) > -1) {
						if (typeof x == "string" && x != "") {
							x = parseFloat(x);
						}
						if (typeof y == "string" && y != "") {
							y = parseFloat(y);
						}
						if (typeof z == "string" && z != "") {
							z = parseFloat(z);
						}
					}
					if (["x", "x+y", "x-y", "x*y", "x/y", "x^y", "100*x/y", "x-(x*y/100)", "x+(x*y/100)", "(x+y)*z"].indexOf(_formula) > -1) {
						const _formula_2 = "return " + _formula;
						const res = new Function("x", "y", _formula_2)(x, y);
						c[_col] = res;
					} else if (_formula == "ROUND(x)") {
						c[_col] = mathjs.round(x);
					} else if (_formula == "ROUND(x,y)") {
						c[_col] = mathjs.round(x, parseInt(y));
					} else if (_formula == "ROUNDUP(x)") {
						c[_col] = mathjs.ceil(x);
					} else if (_formula == "ROUNDDOWN(x)") {
						c[_col] = mathjs.floor(x);
					} else if (_formula == "ABS(x)") {
						c[_col] = mathjs.abs(x);
					} else if (_formula == "NVL(x,y)") {
						if (x != "None" && x != "") {
							c[_col] = x;
						} else {
							c[_col] = y;
						}
					} else if (_formula == "REPLACE(x,y,z)") {
						const reg = new RegExp(y, 'g');
						c[_col] = x.replace(reg, z);
					} else if (_formula == "length(x)") {
						c[_col] = x.length;
					} else if (_formula == "REGEX(x,y)") {
						const reg = new RegExp(x);
						c[_col] = reg.exec(y);
					} else if (_formula == "REG_REPLACE(x,y,z)") {
						// c[_col] = x.length;
					} else if (_formula == "enwrap(x,y)") {
						// c[_col] = x.length;
					} else if (["year of date x", "month of date x", "day of year of date x", "day of month of date x", "day of week of date x"].indexOf(_formula) > -1) {
						if (_formula == "year of date x") {
							c[_col] = Str_Date_To_DateTime(x).getFullYear();
						} else if (_formula == "month of date x") {
							c[_col] = Str_Date_To_DateTime(x).getMonth() + 1;
						} else if (_formula == "day of month of date x") {
							c[_col] = Str_Date_To_DateTime(x).getDate();
						} else if (_formula == "day of week of date x") {
							c[_col] = Str_Date_To_DateTime(x).getDay();
						} else if (_formula == "day of year of date x") {
							c[_col] = Str_Date_To_DateTime(x).getFullYear();
						}
					} else if (_formula == "date x + y days") {
						// c[_col] = x.length;
					} else if (_formula == "coding_rule") {
						c[_col] = coding_rule(parseInt(x), parseInt(y), parseInt(z));
					}
					if (_type == "int") {
						c[_col] = parseInt(c[_col]);
					} else if (_type == "float") {
						c[_col] = parseFloat(c[_col]);
					} else if (_type == "string") {
						c[_col] = (c[_col]) = "";
					} else if (_type == "boolean") {
						let res = c[_col];
						if (typeof res == "string" && ["False", "false"].indexOf(res) > -1) {
							res = false
						} else if (typeof res == "string" && ["True", "true"].indexOf(res) > -1) {
							res = false
						} else {
							res = Boolean(res);
						}
					} else if (_type == "timestamp") {
						c[_col] = Date.parse(c[_col]);
					} else if (_type == "datestring") {
						const _date = new Date(c[_col]);
						if (_date) {
							const res = _date.getFullYear() + "-" + (_date.getMonth() + 1) + "-" + _date.getDate() + " " + _date.getHours() + ":" + _date.getMinutes() + ":" + _date.getSeconds();
							c[_col] = res;
						}
					}
				});
			});
			return c_data;
		}
	}

	function drop_duplicates(data, fields) {
		let field_value = {};
		fields.forEach((field) => {
			field_value[field] = [];
		});
		let res = [];
		data.forEach((cur, idx) => {
			let flag = true;
			fields.forEach((field) => {
				let _value = cur[field];
				let f_value = field_value[field];
				if (f_value.indexOf(_value) < 1) {
					f_value.push(_value);
					flag = false;
				}
			});
			if (!flag) {
				res.push(cur);
			}
		});
		return res;
	}

	function merge(data_1, data_2, left_on, right_on, on, how) {
		let res = [];
		if (on) {
			if (how == "inner") {
				data_1.forEach((d_1) => {
					const c_v = d_1[on];
					let obj = extend({}, d_1);
					data_2.forEach((d_2) => {
						const c_v_2 = d_2[on];
						if (c_v == c_v_2) {
							extend(obj, d_2);
						}
					});
					res.push(obj);
				});
			}
		}
		return res;
	}

	function drop(data, fields) {
		data.forEach((cur) => {
			fields.forEach((field) => {
				delete cur[field];
			});
		});
		return data;
	}

	function join_1(A, B, join_type, left_key, right_key, row_no, reference_key, reference_value, r_name) {
		let c_left_key, c_right_key, result;
		let c_a = cloneDeep(A);
		let c_b = cloneDeep(B);;
		if (reference_key == "left") {
			c_left_key = cloneDeep(left_key);
			c_a.forEach((cur) => {
				r_name.forEach((_name, idx) => {
					if (_name != "") {
						const r_value = reference_value[idx];
						cur[_name] = cur[r_value];
						delete cur[r_value];
						c_left_key.forEach((_l_key) => {
							if (_l_key == l_value) {
								_l_key = _name;
							}
						})
					}
				});
				return cur;
			});
		} else if (reference_key == "right") {
			c_right_key = cloneDeep(right_key);
			c_b.forEach((cur) => {
				r_name.forEach((_name, idx) => {
					if (_name != "") {
						const r_value = reference_value[idx];
						cur[_name] = cur[r_value];
						delete cur[r_value];
						c_right_key.forEach((_r_key, r_idx) => {
							if (_r_key == r_value) {
								_r_key = _name;
							}
						})
					}
				});
				return cur;
			});
		} else {
			console.error("Please give the right parameters!!!");
		}
		if (join_type == "inner") {
			if (row_no == 1) {
				c_a = drop_duplicates(c_a, left_key);
				c_b = drop_duplicates(c_b, right_key);
			}
			result = merge(c_a, c_b, c_left_key, c_right_key, "", "inner");
		} else if (join_type == "left") {
			if (row_no == 1) {
				c_b = drop_duplicates(c_b, right_key);
			}
			result = merge(c_a, c_b, c_left_key, c_right_key, "", "left");
		} else if (join_type == "right") {
			if (row_no == 1) {
				c_a = drop_duplicates(c_a, left_key);
			}
			result = merge(c_a, c_b, c_left_key, c_right_key, "", "right");
		} else if (join_type == "outer") {
			result = merge(c_a, c_b, c_left_key, c_right_key, "", "outer");
		} else if (join_type == "cross") {
			if (c_a.length == 0 && c_b.length == 0) {
				result = [];
			} else if (c_a.length == 0 || c_b.length == 0) {
				const _left_key = Object.keys(c_a[0])[0];
				const _right_key = Object.keys(c_b[0])[0];
				result = merge(c_a, c_b, _left_key, _right_key, "", "outer");
			} else {
				c_a.forEach((cur) => {
					cur["key_temp"] = 1;
				});
				c_b.forEach((cur) => {
					cur["key_temp"] = 1;
				});
				result = merge(c_a, c_b, "", "", "key_temp", "inner");
				result = drop(result, ['key_temp']);
			}
		} else {
			console.error("Please give the right join_type!!!");
		}
		return result;
	}

	function join(condition) {
		return function (data_1, data_2, join_type, left_key, right_key, row_no, reference_key, reference_value, r_name, left_data, right_data) {
			let A, B;
			if (left_data == data_1[1]) {
				A = data_1[0]
				B = data_2[0]
			} else if (left_data == data_2[1]) {
				A = data_2[0]
				B = data_1[0]
			}
			return join_1(A, B, join_type, left_key, right_key, row_no, reference_key, reference_value, r_name);
		}
	}

	function saveToDatabase(condition) {
		return function (data, table_name, type, spaceid, systemid) {
			if (['delete', 'insert', 'update'].indexOf(type) > -1) {
				let out_put = {}, args = [];
				out_put["spaceid"] = spaceid;
				out_put["systemid"] = systemid;
				out_put["argjson"] = args;
				let dic_element = {};
				if (type == "delete") {
					dic_element["tname"] = table_name;
					dic_element["opflag"] = 2;
					dic_element["criteria"] = data[0];
				} else if (type == "insert") {
					dic_element["tname"] = table_name;
					dic_element["opflag"] = 4;
					dic_element["rowdata"] = data[0];
					args.push(dic_element);
				} else if (type == "update") {
					dic_element["tname"] = table_name;
					dic_element["opflag"] = 8;
					dic_element["criteria"] = data[0]["_id"];
					dic_element["rowdata"] = data[0];
				}
				const res = send_msg("dbacc.save.intranet", out_put);
				console.log("444444444444------------------------444444444444444444");
				return res;
			} else {
				console.error(``)
			}
		}
	}

	const feidao = {
		dataInput: dataInput(condition),
		readFromDatabase: readFromDatabase(condition),
		generateVariable: generateVariable(condition),
		link: link(condition),
		get_param_value: get_param_value(condition),
		merge_param: merge_param(condition),
		cutRow: cutRow(condition),
		cutColumn: cutColumn(condition),
		generateRow: generateRow(condition),
		generateColumn: generateColumn(condition),
		addConstant: addConstant(condition),
		attr_params: attr_params(condition),
		params_format: params_format(condition),
		addComputeColumn: addComputeColumn(condition),
		join: join(condition),
		saveToDatabase: saveToDatabase(condition)
	};

	const dsk = {
		"dataInput_1": {
			"dataInput": ["param", "commodity_pricing_no"]
		},
		"dataInput_4": {
			"dataInput": ["param", "usercode"]
		},
		"dataInput_7": {
			"dataInput": ["param", "purchase_num"]
		},
		"generateVariable_2": {
			"generateVariable": ["dataInput_1", "variable", "commodity_pricing_no", "commodity_pricing_no", "string", "first"]
		},
		"generateVariable_5": {
			"generateVariable": ["dataInput_4", "variable", "usercode", "usercode", "string", "first"]
		},
		"generateVariable_8": {
			"generateVariable": ["dataInput_7", "variable", "purchase_num", "purchase_num", "int", "first"]
		},
		"fields1": { "district_name": 1, "real_name": 1, "cust_name": 1, "report_phone": 1, "usercode": 1, "client_no": 1, "_id": 1, "contact_address": 1, "email_address": 1 },
		"get_param_value1": {
			"get_param_value": ["usercode", "generateVariable_5"]
		},
		"criteria1": {
			"merge_param": ["get_param_value1"]
		},
		"readFromDatabase_1": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "customer", "fields1", "criteria1", 1, 20, {}, "sql"]
		},
		"generateVariable_9": {
			"generateVariable": ["readFromDatabase_1", "variable", "client_no", "client_no", "string", "first"]
		},
		"fields2": { "commodity_pricing_no": 1, "inventory": 1, "product_id": 1, "commodity_sale_no": 1, "buy_quota": 1 },
		"get_param_value2": {
			"get_param_value": ["commodity_pricing_no", "generateVariable_2"]
		},
		"criteria2": {
			"merge_param": ["get_param_value2"]
		},
		"readFromDatabase_2": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "commodity_pricing", "fields2", "criteria2", 1, 20, {}, "sql"]
		},
		"generateVariable_10": {
			"generateVariable": ["readFromDatabase_2", "variable", "commodity_sale_no", "commodity_sale_no", "string", "first"]
		},
		"fields3": { "commodity_sale_name": 1, "category_no": 1, "first_category_name": 1, "first_category_no": 1, "img_type": 1, "putaway_time": 1, "business_name": 1, "commodity_sale_no": 1, "img_url": 1, "store_no": 1, "stores_operating": 1, "product_id": 1, "comodity_state": 1, "store_name": 1, "price": 1, "second_category_no": 1, "business_no": 1, "original_price": 1, "category_name": 1, "second_category_name": 1, "description": 1, "_id": 1, "picture_detail": 1, "priority": 1, "instructions": 1 },
		"get_param_value3": {
			"get_param_value": ["commodity_sale_no", "generateVariable_10"]
		},
		"criteria3": {
			"merge_param": ["get_param_value3"]
		},
		"readFromDatabase_3": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "commodity_sale", "fields3", "criteria3", 1, 20, {}, "sql"]
		},
		"cut_fields": ["commodity_sale_no", "commodity_sale_name", "business_no", "business_name", "img_url"],
		"cutColumn_1": {
			"cutColumn": ["readFromDatabase_3", "cut_fields"]
		},
		"generateVariable_11": {
			"generateVariable": ["readFromDatabase_1", "variable", "cust_name", "cust_name", "string", "first"]
		},
		"fields4": { "is_sales_price": 1, "commodity_pricing_no": 1, "is_enable": 1, "price": 1, "field_no": 1, "_id": 1, "field_title": 1, "product_id": 1, "field_name": 1 },
		"get_param_value4": {
			"get_param_value": ["commodity_pricing_no", "generateVariable_2"]
		},
		"get_param_value5": {
			"get_param_value": ["is_enable", true]
		},
		"criteria4": {
			"merge_param": ["get_param_value4", "get_param_value5"]
		},
		"readFromDatabase_4": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "commodity_pricing_price", "fields4", "criteria4", 1, 20, {}, "sql"]
		},
		"condition1": { "is_sales_price": true },
		"cutRow_1": {
			"cutRow": ["readFromDatabase_4", "condition1", 1]
		},
		"condition2": { "is_sales_price": false },
		"cutRow_2": {
			"cutRow": ["readFromDatabase_4", "condition2", 1]
		},
		"generateVariable_12": {
			"generateVariable": ["cutRow_1", "variable", "price", "price", "float", "first"]
		},
		"generateVariable_13": {
			"generateVariable": ["cutRow_2", "variable", "price", "price", "float", "first"]
		},
		"generateRow_1": {
			"generateRow": [1, ["_id", "clear_num", "unit", "selectflag"], ["ss", "", "", "false"], ["string", "string", "string", "boolean"]]
		},
		"generateColumn_1": {
			"generateColumn": ["generateRow_1", ["_id", "join_time"], ["string", "string"], ["GUID", "系统日期"]]
		},
		"params_get": {
			"attr_params": ["generateVariable_9", "generateVariable_8", "generateVariable_12", "generateVariable_13", "generateVariable_11", "generateVariable_2", "generateVariable_15", "generateVariable_17", "generateVariable_18"]
		},
		"addConstant_1": {
			"addConstant": ["generateColumn_1", ["client_no", "purchase_num", "unitprice", "original_price", "nick_name", "commodity_pricing_no", "manufacturer", "retailer_store_no", "retailer_store_name"], ["string", "int", "float", "float", "string", "string", "string", "string", "string"], "params_get"]
		},
		"fields5": { "itemname": 1, "itemvalue": 1, "matcode": 1 },
		"get_param_value6": {
			"get_param_value": ["matcode", "generateVariable_2"]
		},
		"get_param_value7": {
			"get_param_value": ["itemname", "可用库存"]
		},
		"criteria5": {
			"merge_param": ["get_param_value6", "get_param_value7"]
		},
		"readFromDatabase_5": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "z001_time", "fields5", "criteria5", 1, 20, {}, "sql"]
		},
		"generateVariable_14": {
			"generateVariable": ["readFromDatabase_5", "variable", "itemvalue", "itemvalue", "int", "first"]
		},
		"fields6": { "field_value": 1, "commodity_sale_no": 1, "field_name": 1 },
		"get_param_value8": {
			"get_param_value": ["commodity_sale_no", "generateVariable_10"]
		},
		"get_param_value9": {
			"get_param_value": ["field_name", "manufacturer"]
		},
		"criteria6": {
			"merge_param": ["get_param_value8", "get_param_value9"]
		},
		"readFromDatabase_6": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "commodity_sale_fields", "fields6", "criteria6", 1, 20, {}, "sql"]
		},
		"generateVariable_15": {
			"generateVariable": ["readFromDatabase_6", "variable", "field_value", "field_value", "string", "first"]
		},
		"params_get2": {
			"attr_params": ["generateVariable_13", "generateVariable_12"]
		},
		"generateRow_2": {
			"generateRow": ["1", ["original_price", "unitprice"], "params_get2", ["float", "float"]]
		},
		"params_format1": {
			"params_format": ['x=DF["original_price"];y=DF["unitprice"];z=""']
		},
		"attr_params1": {
			"attr_params": ["params_format1"]
		},
		"addComputeColumn_1": {
			"addComputeColumn": ["generateRow_2", ["cha"], ["float"], ["x-y"], "attr_params1"]
		},
		"generateVariable_16": {
			"generateVariable": ["addComputeColumn_1", "variable", "cha", "cha", "float", "first"]
		},
		"generateVariable_17": {
			"generateVariable": ["readFromDatabase_3", "variable", "store_no", "store_no", "string", "first"]
		},
		"fields7": { "longitude": 1, "retailer_store_no": 1, "phone": 1, "store_address": 1, "retailer_store_name": 1, "is_default": 1, "picture": 1, "_id": 1, "latitude": 1, "business_name": 1, "business_no": 1 },
		"get_param_value10": {
			"get_param_value": ["retailer_store_no", "generateVariable_17"]
		},
		"criteria7": {
			"merge_param": ["get_param_value10"]
		},
		"readFromDatabase_7": {
			"readFromDatabase": [1, "medicine_store2017", "1092", "retailer_store", "fields7", "criteria7", 1, 20, {}, "sql"]
		},
		"generateVariable_18": {
			"generateVariable": ["readFromDatabase_7", "variable", "retailer_store_name", "retailer_store_name", "string", "first"]
		},
		"params_format2": {
			"params_format": ['x=" {} ";y=" {} ";z=""', "generateVariable_12", "generateVariable_8"]
		},
		"params_format3": {
			"params_format": ['x=" {} ";y=" {} ";z=""', "generateVariable_16", "generateVariable_8"]
		},
		"params_format4": {
			"params_format": ['x=" {} ";y="";z=""', "generateVariable_14"]
		},
		"attr_params2": {
			"attr_params": ["params_format2", "params_format3", "params_format4"]
		},
		"addComputeColumn_2": {
			"addComputeColumn": ["addConstant_1", ["amount", "given_money", "commodity_count"], ["float", "float", "int"], ["x*y", "x*y", "x"], "attr_params2"]
		},
		"join_field": ['commodity_sale_no', 'commodity_sale_name', 'business_no', 'business_name', 'img_url'],
		"data_1": {
			"attr_params": ["cutColumn_1", "列切_1_shangjia"]
		},
		"data_2": {
			"attr_params": ["addComputeColumn_2", "增加计算列_2"]
		},
		"join_1": {
			"join": ["data_1", "data_2", "cross", [], [], 1, 'left', "join_field", ['', '', '', '', 'image'], '列切_1_shangjia', '增加计算列_2']
		},
		"saveToDatabase_1": {
			"saveToDatabase": ["join_1", "w_shopping_cart", "insert", "medicine_store2017", "1091"]
		},
		"r": {
			"link": ["saveToDatabase_1"]
		}
	}
	return dask.get(dsk, "r", feidao);
}

const data = { "param": { "commodity_pricing_no": "DP2017022800000004", "usercode": "guest", "purchase_num": 2 } };
atom(data);